Completed
Push — master ( 40c2b4...09bd06 )
by Justin
01:29
created

Relationship.getFacts   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
1
var Subject = require('./Subject'),
2
    Fact = require('./Fact'),
3
    ResourceReference = require('./ResourceReference'),
4
    utils = require('./utils');
5
    
6
/**
7
 * A relationship.
8
 * 
9
 * @constructor
10
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
11
 */
12
var Relationship = function(json){
13
  
14
  // Protect against forgetting the new keyword when calling the constructor
15
  if(!(this instanceof Relationship)){
16
    return new Relationship(json);
17
  }
18
  
19
  // If the given object is already an instance then just return it. DON'T copy it.
20
  if(Relationship.isInstance(json)){
21
    return json;
22
  }
23
  
24
  Subject.call(this, json);
25
  
26
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
27
    this.setType(json.type);
28
    this.setPerson1(json.person1);
29
    this.setPerson2(json.person2);
30
    this.setFacts(json.facts);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
31
  }
32
};
33
34
Relationship.prototype = Object.create(Subject.prototype);
35
36
Relationship._gedxClass = Relationship.prototype._gedxClass = 'GedcomX.Relationship';
37
38
/**
39
 * Check whether the given object is an instance of this class.
40
 * 
41
 * @param {Object} obj
42
 * @returns {Boolean}
43
 */
44
Relationship.isInstance = function(obj){
45
  return utils.isInstance(obj, this._gedxClass);
46
};
47
48
/**
49
 * Get the relationship type
50
 * 
51
 * @returns {String} type
52
 */
53
Relationship.prototype.getType = function(){
54
  return this.type;
55
};
56
57
/**
58
 * Set the relationship type
59
 * 
60
 * @param {String} type
61
 * @returns {Relationship} This instance
62
 */
63
Relationship.prototype.setType = function(type){
64
  this.type = type;
65
  return this;
66
};
67
68
/**
69
 * Get person1 reference
70
 * 
71
 * @returns {ResourceReference}
72
 */
73
Relationship.prototype.getPerson1 = function(){
74
  return this.person1;
75
};
76
77
/**
78
 * Set the person1 reference
79
 * 
80
 * @param {ResourceReference} person1
81
 * @returns {Relationship} This instance
82
 */
83
Relationship.prototype.setPerson1 = function(person1){
84
  if(person1){
85
    this.person1 = ResourceReference(person1);
86
  }
87
  return this;
88
};
89
90
/**
91
 * Get person2 reference
92
 * 
93
 * @returns {ResourceReference}
94
 */
95
Relationship.prototype.getPerson2 = function(){
96
  return this.person2;
97
};
98
99
/**
100
 * Set the person1 reference
101
 * 
102
 * @param {ResourceReference} person2
103
 * @returns {Relationship} This instance
104
 */
105
Relationship.prototype.setPerson2 = function(person2){
106
  if(person2){
107
    this.person2 = ResourceReference(person2);
108
  }
109
  return this;
110
};
111
112
/**
113
 * Get the facts
114
 * 
115
 * @return {Fact[]}
116
 */
117
Relationship.prototype.getFacts = function(){
118
  return this.facts || [];
119
};
120
121
/**
122
 * Set the facts
123
 * 
124
 * @param {Fact[]|Object[]} facts
125
 * @returns {Relationship} This instance
126
 */
127
Relationship.prototype.setFacts = function(facts){
128
  return this._setArray(facts, 'facts', 'addFact');
129
};
130
131
/**
132
 * Add a fact
133
 * 
134
 * @param {Fact|Object} fact
135
 * @returns {Relationship} This instance
136
 */
137
Relationship.prototype.addFact = function(fact){
138
  return this._arrayPush(fact, 'facts', Fact);
139
};
140
141
/**
142
 * Export the object as JSON
143
 * 
144
 * @return {Object} JSON object
145
 */
146
Relationship.prototype.toJSON = function(){
147
  return this._toJSON(Subject, [
148
    'type',
149
    'person1',
150
    'person2',
151
    'facts'
152
  ]);
153
};
154
155
module.exports = Relationship;